//
//  EMDoCommonViewController.m
//  ElectronicMall
//
//  Created by 史伟文 on 15/2/4.
//  Copyright (c) 2015年 cczu. All rights reserved.
//

#import "EMDoCommonViewController.h"
#import "UINavigationController+EMNavi.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"
#import "EMOrderCell.h"
#import "EMGoodInfo3.h"
#import "UIImageView+WebCache.h"
#import "EMDoCommomReqBody.h"
#import "EMDoCommomRespBody.h"
#import "UserInfo.h"
@interface EMDoCommonViewController ()
{
    AppDelegate *app;
    UITextView *tv;
}
@end

@implementation EMDoCommonViewController
- (void)viewWillAppear:(BOOL)animated
{
    
    
    [self.navigationController setNavigationMode:EMNavigationModeDefault andSubMode:0];
    self.navigationItem.title = @"商品评价";
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 30, 32);
    [button setImage:[UIImage imageNamed:@"button_导航_返回_白"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:button];
    
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [super viewWillAppear:animated];
}

- (void)backAction: (UIButton *)sender
{
    self.navigationController.navigationBar.barTintColor = RGB(255, 255, 255);
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITableView *commomTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    commomTable.delegate = self;
    commomTable.dataSource = self;
    commomTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:commomTable];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [commomTable addGestureRecognizer:tapGR];
}

- (void)handupCommom:(UIButton *)sender
{
    if (tv.text == nil || tv.text.length == 0) {
        [app showTip:@"评价不能为空"];
        return;
    }
    
    [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
    EMDoCommomReqBody *reqBody = [[EMDoCommomReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"PUBLISH-COMMENT";
    reqBody.USERID = [UserInfo sharedUserInfo].m_userName;
    reqBody.SESSION = [UserInfo sharedUserInfo].m_session;
    reqBody.ORDERNO = self.orderNumber;
    reqBody.ID = _goodInfo.m_id;
    reqBody.CONTENT = tv.text;
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    // 模型转字典
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        // 将返回的字典转成模型，键的名称必须相同
        EMDoCommomRespBody *respBody = [EMDoCommomRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            
            [app showTip:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
            
        } else {
            
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {

        static NSString *cellIdentifier = @"commomCell";
        EMOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[EMOrderCell alloc] initWithFrame:CGRectZero];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.goodNameLabel.text = _goodInfo.m_name;
        cell.goodDescLabel.text = (NSNull *)_goodInfo.m_goodDesc == [NSNull null] ? @"" : _goodInfo.m_goodDesc;;
        if ([_goodInfo.m_mode isEqualToString:@"0"]) {
            cell.nowPriceLabel.text = [NSString stringWithFormat:@"¥%@", _goodInfo.m_nowPrice];
            cell.originPriceLabel.text = @"";
        } else {
            cell.nowPriceLabel.text = [NSString stringWithFormat:@"¥%@", _goodInfo.m_nowPrice];
            NSMutableAttributedString *oPrice = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥%@", _goodInfo.m_originPrice]];
            [oPrice addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, _goodInfo.m_originPrice.length+1)];
            cell.originPriceLabel.attributedText = oPrice;
        }
        
        [cell.goodImgView sd_setImageWithURL:[NSURL URLWithString:_goodInfo.m_imgUrl]];
        cell.commonButton.hidden = YES;
        return cell;
    } else if (indexPath.section == 1) {
        static NSString *cellIdentifier = @"commomCCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:cellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        tv = [[UITextView alloc] initWithFrame:CGRectMake(W(10), 0, W(300), H(120))];
        tv.layer.cornerRadius = 5;
        tv.layer.borderWidth = 1;
        tv.layer.borderColor = [UIColor grayColor].CGColor;
        tv.text = @"请输入评价";
        tv.delegate = self;
        [cell addSubview:tv];
        return cell;
    } else {
        
        static NSString *cellIdentifier = @"commomConCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:cellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(W(40), H(5), W(240), H(40));
        btn.backgroundColor = RGB(112, 179, 81);
        btn.layer.cornerRadius = 5;
        btn.titleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:20];
        [btn setTitle:@"提交评论" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(handupCommom:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:btn];
        return cell;
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 90;
    } else if (indexPath.section == 1) {
        return H(120);
    } else {
        return 350;
    }
}

- (void)tapAction:(UIGestureRecognizer *)gestureRecognize
{
    [tv resignFirstResponder];
}
@end
